package org.sport.dbImplement;

import org.sport.dbInterface.UserInfoHisOperation;
import org.sport.model.entityModel.UserinfohistorytableEntity;
import org.sport.model.entityModel.UsertableEntity;
import org.sport.repository.UserinfohistoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Allen on 2017/9/20.
 *
 * 有两个查询返回暂时没有做，并不是所有接口都需要
 */

@Service
public class UserInfoHisOpeImp implements UserInfoHisOperation {


    @Autowired
    UserinfohistoryRepository userinfohistoryRepository;

    @Override
    public List<UserinfohistorytableEntity> findAllInfoHisByUserId(UsertableEntity user) {
        return userinfohistoryRepository.findAllUserPriByUserId(user.getId());
    }

    @Override
    public UserinfohistorytableEntity findLatestInfoHisByUserId(UsertableEntity user) {


        int page = 0,size = 1;
        Sort sort = new Sort(Sort.Direction.DESC,"id");
        Pageable pageable = new PageRequest(page,size,sort);
        Page<UserinfohistorytableEntity>    userinfhisdatapage  =   userinfohistoryRepository.findLastUserPriByUserId(user.getId(),pageable);
        List<UserinfohistorytableEntity>    userinfhisList      =   userinfhisdatapage.getContent();
/*        Iterator it1 = userinfhisList.iterator();
        while(it1.hasNext())
        {
            System.out.println(((UserinfohistorytableEntity)it1.next()).getId());
        }*/
        return userinfhisList.get(0);
    }

    @Override
    public UserinfohistorytableEntity findInfoHisByUserIdByTime(UserinfohistorytableEntity userinfohistorytableEntity) {
        if(userinfohistorytableEntity.getUsertableByUserId() == null || userinfohistorytableEntity.getTime() == null)
        {
            System.out.println("外键为空或者时间没有填写");
            return null;
        }
        else
        {
            Integer     UserId      = userinfohistorytableEntity.getUsertableByUserId().getId();
            Timestamp   timestamp   = userinfohistorytableEntity.getTime();
            return userinfohistoryRepository.findUserPriByUserIdAndTime(UserId,timestamp);
        }

    }

    @Override
    public List<UserinfohistorytableEntity> findAllInfoHis() {
        return userinfohistoryRepository.findAll();
    }

    @Override
    public boolean addUserInfoHisOne(UserinfohistorytableEntity userinfohis) {
        if(userinfohistoryRepository.saveAndFlush(userinfohis) != null)
            return true;
        else
            return false;
    }

    @Override
    public boolean delUserInfoHisOne(UserinfohistorytableEntity userinfohis) {

        if(userinfohis.getUsertableByUserId() != null)
        {
            userinfohis = findInfoHisByUserIdByTime(userinfohis);
            if(userinfohis == null)                                             //首先查询是否有这样一条数据
            {
                System.out.println("没有找到对应的数据");
            }
            else {
//                System.out.println(userinfohis.toString());

                userinfohistoryRepository.delete(userinfohis);                                      //查到了再删除
                UserinfohistorytableEntity userinfohis2 = findInfoHisByUserIdByTime(userinfohis);   //看是否删除成功
                if (userinfohis2 != null)
                    return false;
                else
                    return true;
            }
        }
        else
        {
            System.out.printf("传入的user为null");
            return false;
        }
        return false;
    }

    @Override
    public boolean updateUserInfoHisOne(UserinfohistorytableEntity userinfohis) {

        Double weight                       =   userinfohis.getWeight();
        Double bodyFatRate                  =   userinfohis.getBodyFatRate();
        String otherData                    =   userinfohis.getOtherData();
        UsertableEntity usertableByUserId   =   userinfohis.getUsertableByUserId();
        userinfohistoryRepository.updateUserInfoHis(weight,
                                                    bodyFatRate,
                                                    otherData,
                                                    usertableByUserId);
        return true;
    }
}
